package com.qianfeng.gl4study.youshare.acitivity.store;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.model.Product;
import com.qianfeng.gl4study.youshare.utils.*;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/23
 * Email:pdsfgl@live.com
 */
public class CoreProductInfoActivity extends Activity implements View.OnClickListener{

	private Product product;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_core_product_information);

		ImageView imageView = (ImageView) findViewById(R.id.info_product_core_image);
		ImageView goodsImage = (ImageView) findViewById(R.id.info_product_core_img_bg_down);
		TextView txtPriceYoushare = (TextView) findViewById(R.id.info_product_core_price);
		TextView txtName = (TextView) findViewById(R.id.info_product_core_name);
		TextView txtBrief = (TextView) findViewById(R.id.info_product_core_brief);
		Button coreBuy = (Button) findViewById(R.id.btn_info_core_product_buy);
		Button coreCollect = (Button) findViewById(R.id.btn_info_core_product_collect);

		coreBuy.setOnClickListener(this);
		coreCollect.setOnClickListener(this);
		Intent intent = getIntent();
		Serializable pro = intent.getSerializableExtra("product");
		if(pro instanceof Product){
			product = (Product) pro;
			//显示商品信息
			txtPriceYoushare.setText(product.getShopPrice());
			txtName.setText(product.getAppGname());
			txtBrief.setText(product.getGoodsBrief());
			/**
			 *开启异步任务
			 */
			//获取缩略图，采用相对路径，进行网址的处理
			OnLoaderImage(product.getGoodsThumb(),imageView);
			//获取商品图片
		//	OnLoaderImage(product.getGoodsImg(),goodsImage);

		}
	}

	private void OnLoaderImage(String thumb,ImageView imageView){
		if(thumb!=null&&thumb.length()>0) {
			if (!thumb.startsWith("http://") && !thumb.startsWith("https://")) {
				thumb = Constants.IMAGE_SERVICE + thumb;
			}
			//进行图片下载之前设置ImageView的tag，异步任务检查tag
			imageView.setTag(thumb);

			//进行内存检查，如果有图片直接使用
			ImageCache imageCache = ImageCache.getInstance();
			Bitmap bitmap = imageCache.getImage(thumb);
			if(bitmap!=null){
				//包含图片对象，直接使用
				imageView.setImageBitmap(bitmap);
			}else {
				//不包含缓存，进行本地文件的加载
				FileCache fileCache = FileCache.getInstance();
				byte[] bytes = fileCache.getContent(thumb);
				if(bytes!=null&&bytes.length>0){

					Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
					imageView.setImageBitmap(bmp);
					imageCache.putImage(thumb,bmp);
				}else {
					ImageLoader imageLoader = new ImageLoader(imageView);
					imageLoader.execute(thumb);
				}
			}
		}
	}

	/**
	 * 单击购买或收藏时调用
	 * @param v
	 */
	@Override
	public void onClick(View v) {

		int id = v.getId();
		if(id==R.id.btn_info_core_product_buy){
			/*
			//点击购买
			collectProduct();
			Toast.makeText(this, "添加购物车完成", Toast.LENGTH_LONG).show();
			*/

			//跳转到购买页面
			Intent intent = new Intent(this, PlatformProductInfoActivity.class);
			intent.putExtra("product",product);
			startActivity(intent);
			finish();

		}else if(id==R.id.btn_info_core_product_collect){

			/*
			//点击收藏
			collectProduct();
			Toast.makeText(this, "收藏完成", Toast.LENGTH_LONG).show();
			*/
			//跳转到购买页面
			Intent intent = new Intent(this, PlatformProductInfoActivity.class);
			intent.putExtra("product",product);
			startActivity(intent);
			finish();

		}
		/*
		//收藏完成后，跳转到商品列表
		Intent intent = new Intent(this, StoreActivity.class);
		startActivity(intent);
		finish();
		*/
	}

	/**
	 * 收藏商品
	 * @return
	 */
	/*
	private CollectItem collectProduct(){

		CollectDatabasesManager databasesManager = CollectDatabasesManager.createInstance(this);
		CollectItem collectItem = new CollectItem();
		collectItem.setProductName(product.getName());
		//设置默认价格200元
		collectItem.setProductPrice(200.0f);
		collectItem.setProductThumb(product.getGoodsThumb());
		collectItem.setCount(1);
		collectItem.setProductId(product.getGoodsId());
		collectItem.setChecked(false);
		databasesManager.collectProduct(collectItem,Constants.USER_ID);

		return collectItem;
	}
	*/
}